import Vue from 'vue';
import Vuex from 'vuex';
import request from '../common/request';
Vue.use(Vuex);
const store = new Vuex.Store({
	state: {
		user: {},
		isLogin: false,
		balanceInfo: {
			greenPoints: 0,
		}
	},
	// 修改store中的值唯一的方法就是提交mutation来修改
	mutations: {
		/**
		 * 设置状态
		 * @param {Object} state 状态对象
		 * @param {Object} provider 传入值
		 */
		userState(state, provider) {
			state.user = provider
		},
		balanceInfoState(state, provider){
			state.balanceInfo = provider
		}
	},
	actions: {
		getBalance(context, userId){
			request("/app-api/pay/wallet-transaction/countUserPoints", 'GET', {
				userId: userId
			}).then(res=>{
				if(res.code===0) {
					console.log("积分信息", res.data)
					context.commit("balanceInfoState", res.data)
				}
			})
		},
		
		// 用户信息
		getUser(context){
			request("/app-api/member/user/get", "GET", {}).then(res=>{
				if(res.code===0) {
					console.log("用户信息", res.data)
					context.commit("userState", res.data)
					context.dispatch("getBalance", res.data.id)
				}
			})
		},
		setRedDot(){
			uni.showTabBarRedDot({
				index: 2
			})
		},
		hideRedDot(){
			uni.hideTabBarRedDot({
				index: 2
			})
		},
	}
})

export default store